package com.egg.logger.rabbitmq.listener;

import com.egg.auth.constants.AuthRabbitExchange;
import com.egg.common.core.constant.StringPool;
import com.egg.common.core.constant.rabbit.RabbitRoutingKey;
import com.egg.common.util.JsonUtil;
import com.egg.logger.constant.LogConstant;
import com.egg.logger.model.request.AccessPost;
import com.egg.logger.service.access.IAccessService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * 监听
 *
 * @author east
 */
@Slf4j
@Component
public class AccessListener {


    @Autowired
    private IAccessService accessService;


    /**
     * 监听-示例
     * 本队列 = 本系统 + 生产者渠道 + 生产者KEY
     *
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = LogConstant.SYSTEM_NAME + StringPool.DOT + AuthRabbitExchange.EXCHANGE_ACCESS_LOGIN + StringPool.DOT + RabbitRoutingKey.ADD)
                    , exchange = @Exchange(name = AuthRabbitExchange.EXCHANGE_ACCESS_LOGIN)
                    , key = {RabbitRoutingKey.ADD}
            )
    })
    @Transactional(rollbackFor = Throwable.class)
    public void listener(Message message, Channel channel) {
        AccessPost accessPost = JsonUtil.objToObj(message.getBody(), AccessPost.class);
        accessService.add(accessPost);
    }


}
